﻿using Fleck;
using System;
using System.Collections.Generic;
using System.IO;

namespace WebSocketAndPrint
{
    public class WebSocketHandle
    {
        //WebSocket 集合
        private Dictionary<String, WebSocket> WebSocketDick = new Dictionary<String, WebSocket>();
        private ViewData VD = ViewData.GetInstance();

        public void Start(String ServerURI)
        {
            var server = new WebSocketServer(ServerURI);
            server.Start(socket =>
            {
                socket.OnOpen = () =>
                {
                    WebSocketDick.Add(socket.GetHashCode().ToString(), new WebSocket(socket));
                    VD.PushSocket(new WebSocket(socket,Guid.NewGuid().ToString()));
                };
                socket.OnClose = () =>
                {
                    WebSocketDick.Remove(socket.GetHashCode().ToString());
                    VD.PopSocket(socket.GetHashCode());
                };
                socket.OnMessage = message =>
                {
                    SocketMessage msg = JsonUtil.FromJSON<SocketMessage>(message);
                    Console.WriteLine("收到了" + message);
                    String NickName = VD.GetNickName(socket.GetHashCode());
                    switch (msg.Type)
                    {
                        case "msg":                            
                            foreach (WebSocket s in this.WebSocketDick.Values)
                            {                              
                                s.WebSocketConnection.Send(new SocketMessage(msg.Type, NickName  + ":" + msg.Text).GetJson());
                            }
                            VD.PushTextBox( NickName + ":" + msg.Text);
                            break;
                        case "rename":
                            VD.ReNickName(socket.GetHashCode(), msg.Text);
                            socket.Send(new SocketMessage(msg.Type, VD.GetNickName(socket.GetHashCode()) + " =》改为了=》" + msg.Text).GetJson());
                            VD.PushTextBox( VD.GetNickName(socket.GetHashCode()) + " =》改为了=》" + msg.Text);
                            break;
                        case "getImg":
                            String imgData64 = GenerateImage.GetScreen();
                            //发固定的图片
                            //FileStream fs = File.OpenRead(@"d:\4.png");
                            //int filelength = 0;
                            //filelength = (int)fs.Length; //获得文件长度 
                            //Byte[] image = new Byte[filelength]; //建立一个字节数组 
                            //fs.Read(image, 0, filelength); //按字节流读取
                            //fs.Close();
                            //string imgData64 = Convert.ToBase64String(image);
                            socket.Send(new SocketMessage(msg.Type,imgData64).GetJson());
                            break;
                        default:
                            socket.Send(socket.GetHashCode() + "只能识别[msg][rename]的类型消息");
                            break;
                    }
                };
            });
        }

    }

}
